
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

#include "DLine.h"

DLine::DLine(void)
{
	
}

DLine::DLine(int x, int y, int xx, int yy)
{
	x1 = x;
	y1 = y;
	x2 = xx;
	y2 = yy;
}

DLine::~DLine(void)
{
}

void DLine::Draw(wxDC *dc)
{
	if(m_selected == TRUE)
	{
		this->SetDrawingTools(dc);
		dc->SetPen(wxPen(wxColor(0, 0, 255), 1, wxDOT));
		dc->DrawLine(x1, y1, x2, y2);
	}
	else
	{
		this->SetDrawingTools(dc);
		dc->DrawLine(x1, y1, x2, y2);
	}
}

bool DLine::PointInside(wxPoint *pnt)
{
	int _x1 = x1, _x2 = x2, _y1 = y1,  _y2 = y2;

	float hipo = sqrt(pow((double)(mod(_x2) - mod(_x1)), 2) + pow((double)(mod(_y2) - mod(_y1)), 2));
	float cosinus = mod(_x2 - _x1) / hipo;
	float sinus = mod(_y2 - _y1) / hipo;

	int quart = 0;
	if(_x1 <= _x2 && _y1 <= _y2)
		quart = 1;
	if(_x1 >= _x2 && _y1 <= _y2)
		quart = 2;
	if(_x1 >= _x2 && _y1 >= _y2)
		quart = 3;
	if(_x1 <= _x2 && _y1 >= _y2)
		quart = 4;

	wxPoint base;
	base.x = 1;
	base.y = 0;

	cosinus = ((_x2 - _x1) * base.x + (_y2 - _y1) * 0) / (sqrt(pow((double)(_x2 - _x1), 2) + pow((double)(_y2 - _y1), 2)) * sqrt(pow((double)base.x, 2) + pow((double)0, 2)));
	sinus = sqrt(1 - cosinus * cosinus);

	//float arccosi = acos(cosinus);
	
	wxPoint clpr;
	switch(quart)
	{
	case 1:
		clpr.x = ((pnt->x - _x1) * cosinus + (pnt->y - _y1) * sinus);
		clpr.y = ((pnt->x - _x1) * -sinus + (pnt->y - _y1) * cosinus);
		break;
	case 2:
		clpr.x = ((pnt->x - _x1) * cosinus + (pnt->y - _y1) * sinus);
		clpr.y = ((pnt->x - _x1) * -sinus + (pnt->y - _y1) * cosinus);
		break;
	case 3:
		clpr.x = ((pnt->x - _x1) * cosinus + (pnt->y - _y1) * (sinus));
		clpr.y = ((pnt->x - _x1) * -(sinus) + (pnt->y - _y1) * -cosinus);
		break;
	case 4:
		clpr.x = ((pnt->x - _x1) * cosinus + (pnt->y - _y1) * sinus);
		clpr.y = (((pnt->x - _x1) * -sinus + (pnt->y - _y1) * -cosinus));
		break;
	default:
		break;
	}

	wxRect rct;
	rct.x = 0;
	rct.y = -5;
	rct.width = hipo;
	rct.height = 10;

	if(rct.Inside(clpr))
		return true;
	else
		return false;
}

float DLine::GetArea()
{
	float hipo = sqrt(pow((double)(mod(x2) - mod(x1)), 2) + pow((double)(mod(y2) - mod(y1)), 2));
	return hipo * 10.f;
}


void DLine::ConvertToText(wxString *str)
{
	*str << wxT("line") << wxT("\t") << x1 << wxT("\t") << y1 << wxT("\t") << x2 << wxT("\t") << y2  << wxT("\n");
}

wxPoint DLine::GetTopLeft(void)
{
	wxPoint pnt;
	if(x1 > x2)
		pnt.x = x2;
	else
		pnt.x = x1;
	if(y1 > y2)
		pnt.y = y2;
	else
		pnt.y = y1;

	return pnt;
}

wxPoint DLine::GetBottomRight(void)
{
	wxPoint pnt;
	if(x1 > x2)
		pnt.x = x1;
	else
		pnt.x = x2;
	if(y1 > y2)
		pnt.y = y1;
	else
		pnt.y = y2;

	return pnt;
}

int DLine::X1() { return x1; }
int DLine::Y1() { return y1; }
int DLine::X2() { return x2; }
int DLine::Y2() { return y2; }

void DLine::SetPosition(int _x1, int _y1, int _x2, int _y2) { x1 = _x1; x2 = _x2; y1 = _y1; y2 = _y2; }